import { changeForm } from '@u/form.js';
import verify from './verify.js';
import getEnums from './enum.js';

export default {
  data() {
    return {
      id: this.$route.params.id,
      accountName: '',
      activityName: '',
      formObj: {
        accountId: 0,
        activityId: 0,
      },
      opts: {
        accountId: {
          label: '会员名称：',
          slot: true,
          required: true,
          position: [1, 1],
        },
        goods: {
          label: '物品：',
          value: '',
          required: true,
          attrs: {
            placeholder: '请输入物品名称',
          },
          position: [2, 1],
        },
        money: {
          label: '货币：',
          value: '',
          required: true,
          attrs: {
            placeholder: '请输入金额',
          },
          events: {
            input: this.setIntegral,
          },
          directives: {
            input: /0|[1-9]\d*/,
          },
          position: [],
        },
        integral: {
          label: '积分：',
          value: '',
          className: 'disable',
          attrs: {
            suffix: '分',
            readonly: true,
          },
          removeTab: true,
          position: [3, 1],
        },
        activityId: {
          label: '贡献来源：',
          slot: true,
          position: [4, 1],
        },
        type: {
          label: '贡献类型：',
          value: 1,
          required: true,
          type: 'radio',
          options: [
            { id: 1, name: '物品' },
            { id: 2, name: '货币' },
          ],
          position: [1, 2],
        },
        estimated: {
          label: '物品预估价值：',
          value: '',
          required: true,
          labelWidth: '125px',
          attrs: {
            suffix: '元',
          },
          events: {
            input: this.setIntegral,
          },
          directives: {
            input: /0|[1-9]\d*/,
          },
          position: [2, 2],
        },
        time: {
          label: '贡献时间：',
          value: '',
          required: true,
          type: 'date',
          attrs: {
            type: 'datetime',
          },
          position: [3, 2],
        },
      },
      cancelMessage: {
        msg: '确定取消吗？',
        title: '取消确认',
        options: {}
      },
      cancelNavTo: { path: '' },
      confirmNavTo: { path: '' }
    };
  },
  watch: {
    'opts.type.value': {
      handler(newVal) {
        this.changeType(newVal);
      },
      immediate: true,
    }
  },
  async created() {
    if (this.id) {
      await this.detailFn(this.id).then(res => {
        if (res.code == 200) {
          // console.log(res.data);
        }
      });
    }
    getEnums(this);
  },
  activated() {
    this.$store.commit('layout/initManuallyRefresh', async() => {
      getEnums(this);
    });
  },
  methods: {
    changeType(typeId) {
      const obj = {
        goods: [[2, 1], []],
        money: [[], [2, 1]],
        estimated: [[2, 2], []],
        integral: [[3, 1], [2, 2]],
        activityId: [[4, 1], [3, 2]],
        time: [[3, 2], [3, 1]],
      };
      for (const k in obj) {
        switch (typeId) {
          case 1:
            this.opts.integral.value = this.opts.estimated.value;
            this.opts[k].position = obj[k][0];
            break;
          case 2:
            this.opts.integral.value = this.opts.money.value;
            this.opts[k].position = obj[k][1];
            break;
        }
      }
    },
    setIntegral(e) {
      setTimeout(() => {
        this.opts.integral.value = e || 0;
      }, 100);
    },
    async confirm() {
      const params = changeForm(this.opts, this.formObj);
      if (verify(this, params)) {
        switch (params.type) {
          case 1:
            params.money = 0;
            break;
          case 2:
            params.goods = '';
            params.estimated = 0;
            break;
        }
        let res = {};
        if (!this.id) {
          res = await this.addFn({ data: params });
          this.confirmNavTo.params.id = res.data.id;
        } else {
          res = await this.editFn(this.id, { data: params });
        }
        if (res.code == 200) {
          this.$router.push(this.confirmNavTo);
        }
      }
    },
    cancel() {
      this.$confirm(this.cancelMessage.msg, this.cancelMessage.title, {
        type: 'warning',
        ...this.cancelMessage.options
      }).then(() => {
        this.navigateBack(this.cancelNavTo);
      });
    }
  },
  components: {
    Form: () => import('@c/Form.vue'),
  }
};
